﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Permissions;
using System.Windows.Forms;

namespace Lib.CommonForm
{
    public class ConnectLog
    {
        private const string _settingsFile = "ConnectString.con";
        private static string _directoryPath = Application.StartupPath;
        private static string _filePath = _directoryPath + "\\" + _settingsFile;

        public string ServiceAddress { get; set; }
        public string ServiceAddressLog { get; set; }
        public string LastUser { get; set; }

        public ConnectLog()
        {
            //Cach 1
            StreamReader reader = new StreamReader(_filePath);
            //Lan luot doc cac dong theo thu tu
            string obj1 = reader.ReadLine();
            string obj2 = reader.ReadLine();
            string obj3 = reader.ReadLine();

            this.ServiceAddress = Split(obj1);
            this.ServiceAddressLog = Split(obj2);
            this.LastUser = Split(obj3);
            
            reader.Close();
            //

            //Cach 2
            //ServiceAddressLog = Lib.CommonForm.Properties.Settings.Default.ServiceAddressLog;
            //ServiceAddress = Lib.CommonForm.Properties.Settings.Default.ServiceAddress;
            //LastUser = Lib.CommonForm.Properties.Settings.Default.LastUser;

        }
        private string Split(object obj)
        {
            if (obj != null)
                if (obj.ToString().Contains("|"))
                    return obj.ToString().Split('|')[1];
            return "";
        }
        public static void WriteLog(string address, string addressLog, string uid)
        {
            //Cach 1
            ////Khai báo để các file trong thư mục này (Attributes) trở về bình thường (Normal) không bị Readonly
            DirectoryInfo di = new DirectoryInfo(_directoryPath);

            ////do your stuff
            StreamWriter writer = new StreamWriter(_filePath);
            writer.WriteLine("ServiceAddress|" + address);
            writer.WriteLine("ServiceAddressLog|" + addressLog);
            writer.WriteLine("LastUser|" + uid);
            writer.Close();
            //

            //Cach 2
            //Lib.CommonForm.Properties.Settings.Default.ServiceAddressLog = addressLog;
            //Lib.CommonForm.Properties.Settings.Default.ServiceAddress = address;
            //Lib.CommonForm.Properties.Settings.Default.LastUser = uid;
            //Lib.CommonForm.Properties.Settings.Default.Save();

        }
    }
}
